package com.dengyafei.controller;


import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.GraduateEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.dto.GraduateInfoDTO;
import com.dengyafei.domin.dto.GraduateIntroductionDTO;
import com.dengyafei.domin.vo.*;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.service.GraduateService;
import com.dengyafei.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邓亚非
 */
@RestController
@RequestMapping("/graduate")
@Slf4j
public class GraduateController {

    @Autowired
    private GraduateService graduateService;

    /**
     * @param graduateRegisterVO
     * @return
     */
    @PostMapping(value = "/graduateRes")
    public RestApiResult graduateRes(@RequestBody GraduateRegisterVO graduateRegisterVO) {
        log.info("【应届生模块-注册接口-参数】graduateRegisterVO:[{}]", graduateRegisterVO);
        Integer integer = graduateService.graduateRegister(graduateRegisterVO);
        System.out.println("integer:" + integer);
        if (integer.equals(RecommendConstant.ALREADY_REGISTER)) {
            log.error("【应届生模块-注册接口-失败】graduateRegisterVO:[{}]", ResultMessage.Graduate.ALREADY_REGISTER);
            return RestApiResult.error(ResultMessage.Graduate.ALREADY_REGISTER);
        }
        if (RecommendConstant.REGISTER_FAILURE.equals(integer)) {
            log.error("【应届生模块-注册接口-失败】graduateRegisterVO:[{}]", ResultMessage.Graduate.REGISTER_FAILURE);
            return RestApiResult.error(ResultMessage.Graduate.REGISTER_FAILURE);
        }
        log.info("【应届生模块-注册接口-成功】graduateRegisterVO:[{}]", ResultMessage.Graduate.REGISTER_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Graduate.REGISTER_SUCCESS);
    }

    /**
     * 应届生登录接口
     *
     * @param graduateLoginVO
     * @return
     */
    @PostMapping(value = "/graduateLogin")
    public RestApiResult graduateLogin(@RequestBody GraduateLoginVO graduateLoginVO) {
        HashMap<String, String> map = new HashMap<>();
        log.info("【应届生模块-登录接口-参数】graduateLoginVO:[{}]", graduateLoginVO);
        Integer integer = graduateService.graduateLogin(graduateLoginVO);
        if (integer.equals(RecommendConstant.NOT_REGISTER)) {
            log.error("【应届生模块-登录接口-失败】graduateLoginVO,cause:[{}]", ResultMessage.Graduate.NOT_REGISTER);
            return RestApiResult.error(ResultMessage.Graduate.NOT_REGISTER);
        } else if (integer.equals(RecommendConstant.PASSWORD_ERROR)) {
            log.error("【应届生模块-登录接口-失败】graduateLoginVO,cause:[{}]", ResultMessage.Graduate.PASSWORD_ERROR);
            return RestApiResult.error(ResultMessage.Graduate.PASSWORD_ERROR);
        }
        log.info("【应届生模块-登录接口-成功】graduateLoginVO:[{}]", ResultMessage.Graduate.LOGIN_SUCCESS);
        map.put("userId", graduateLoginVO.getUsername());
        String token = JwtUtil.generateToken(map);
        return RestApiResult.okMsg(token);
    }

    /**
     * 应届生忘记密码-重置密码
     *
     * @param resetPasswordVO
     * @return
     */
    @PostMapping(value = "/graduateResetPassword")
    public RestApiResult graduateResetPassword(@RequestBody ResetPasswordVO resetPasswordVO) {
        log.info("【应届生模块-重置密码接口-参数】resetPasswordVO:[{}]", resetPasswordVO);
        Integer integer = graduateService.graduateFindPassword(resetPasswordVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【应届生模块-重置密码接口-失败】resetPasswordVO,cause:[{}]", ResultMessage.Graduate.RESET_PASSWORD_FAILURE);
            return RestApiResult.error(ResultMessage.Graduate.RESET_PASSWORD_FAILURE);
        }
        log.info("【应届生模块-重置密码接口-成功】resetPasswordVO:[{}]", ResultMessage.Graduate.RESET_PASSWORD_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Graduate.RESET_PASSWORD_SUCCESS);
    }

    /**
     * 查询个人信息
     *
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryMyIntroduction")
    public RestApiResult queryMyIntroduction() {
        log.info("【应届生模块-查询个人简介接口-参数】:[{}]", "无参");
        GraduateIntroductionDTO graduateIntroductionDTO = graduateService.queryMyIntroduction();
        log.info("【应届生模块-查询个人简介接口-成功】:[{}]", ResultMessage.Graduate.QUERY_SUCCESS);
        return RestApiResult.ok(graduateIntroductionDTO);
    }

    /**
     * 查询个人信息接口
     *
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryByGid")
    public RestApiResult queryByGid() {
        log.info("【应届生模块-查询个人信息接口-参数】:[{}]", "无参");
        GraduateInfoDTO graduateEntity = graduateService.queryByGid();
        log.info("【应届生模块-查询个人信息接口-成功】:[{}]", ResultMessage.Graduate.QUERY_SUCCESS);
        return RestApiResult.ok(graduateEntity);
    }

    /**
     * 更新应届生信息
     *
     * @param graduateUpdateVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/updateGraduateInfo")
    public RestApiResult updateGraduateInfo(@RequestBody GraduateUpdateVO graduateUpdateVO) throws ParseException {
        log.info("【应届生模块-更新接口-参数】:[{}]", graduateUpdateVO);
        Integer integer = graduateService.updateGraduateInfo(graduateUpdateVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【应届生模块-更新接口-失败】graduateUpdateVO,cause:[{}]", ResultMessage.Graduate.UPDATE_FAILURE);
            return RestApiResult.error(ResultMessage.Graduate.UPDATE_FAILURE);
        }
        log.info("【应届生模块-更新接口-成功】graduateUpdateVO:[{}]", ResultMessage.Graduate.UPDATE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Graduate.UPDATE_SUCCESS);
    }

    /**
     * 验证身份
     *
     * @param graduateUpdatePasswordVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/checkIdentify")
    public RestApiResult checkIdentify(@RequestBody GraduateUpdatePasswordVO graduateUpdatePasswordVO) {
        log.info("【应届生模块-验证身份接口-参数】graduateUpdatePasswordVO:[{}]", graduateUpdatePasswordVO);
        GraduateEntity graduateEntity = graduateService.queryByName(graduateUpdatePasswordVO);
        log.info("【应届生模块-验证身份接口-成功】graduateUpdatePasswordVO:[{}]", ResultMessage.Graduate.QUERY_BY_NAME);
        return RestApiResult.ok(graduateEntity);
    }

    /**
     * 修改密码
     *
     * @param graduateUpdatePasswordVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/updatePassword")
    public RestApiResult updatePassword(@RequestBody GraduateUpdatePasswordVO graduateUpdatePasswordVO) {
        log.info("【应届生模块-修改密码接口-参数】graduateUpdatePasswordVO:[{}]", graduateUpdatePasswordVO);
        Integer integer = graduateService.updateGraduatePassword(graduateUpdatePasswordVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【应届生模块-修改密码接口-失败】graduateUpdatePasswordVO,cause:[{}]", ResultMessage.Graduate.UPDATE_PASSWORD_FAILURE);
            return RestApiResult.error(ResultMessage.Graduate.UPDATE_PASSWORD_FAILURE);
        }
        log.info("【应届生模块-修改密码接口-成功】graduateUpdatePasswordVO:[{}]", ResultMessage.Graduate.UPDATE_PASSWORD_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Graduate.UPDATE_PASSWORD_SUCCESS);
    }

    /**
     * 应届生上传头像并通过base64加密保存到redis缓存中
     *
     * @param file
     * @return
     */
    @CrossOrigin
    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        log.info("【应届生模块-上传头像接口-参数】:[{}]", file);
        try {
            String string = Base64Utils.encodeToString(file.getBytes());
            String imgUrl=RecommendConstant.SRC_PREFIX+string;
            log.info("【应届生模块-上传头像接口-成功】:[{}]",ResultMessage.Common.UPLOAD_SUCCESS);
            return imgUrl;
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.error("【应届生模块-上传头像接口-失败】,cause:[{}]",ResultMessage.Common.UPLOAD_FAILURE);
        return null;
    }

    /**
     * 保存图片url到redis缓存
     * @param graduateHeadImageVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/save")
    public RestApiResult save(@RequestBody GraduateHeadImageVO graduateHeadImageVO) throws IOException {
        log.info("【应届生模块-保存头像路径接口-参数】:[{}]",graduateHeadImageVO);
        Boolean aBoolean = graduateService.saveHeadImage(graduateHeadImageVO);
        if (aBoolean.equals(false)){
            log.error("【应届生模块-保存头像路径接口-失败】,cause:[{}]",ResultMessage.Common.SAVE_FAILURE);
            return RestApiResult.error(ResultMessage.Common.SAVE_FAILURE);
        }
        log.info("【应届生模块-保存头像路径接口-成功】:[{}]",ResultMessage.Common.SAVE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Common.SAVE_SUCCESS);
    }

    /**
     * 加载头像
     * @return
     */
    @JwtToken
    @PostMapping(value = "/loadHeadImage")
    public RestApiResult loadHeadImage(){
        log.info("【应届生模块-加载头像接口-参数】:[{}]","无参");
        String s = graduateService.queryImageUrl();
        log.info("【应届生模块-加载头像接口-成功】:[{}]",ResultMessage.Common.LOAD_SUCCESS);
        return RestApiResult.okMsg(s);
    }

    /**
     * 所有应届毕业生信息
     * @return
     */
    @PostMapping(value = "/queryGraduates")
    public RestApiResult queryGraduates(){
        List<GraduateEntity> graduateEntities = graduateService.queryGraduates();
        return RestApiResult.ok(graduateEntities);
    }
}